﻿#nullable enable

using System;
using System.Collections.Generic;

namespace PHMEE.EnergyEfficiency;

public static class VesselTypeHelper
{
    public static string GetEEOIUnitByVesselType(VesselType vesselType)
    {
        var cargoUnit = GetCargoUnitByVesselType(vesselType);
        return $"g CO2/({cargoUnit}·nm)";
    }

    public static string GetCargoUnitByVesselType(VesselType vesselType)
    {
        List<VesselType> cargoMtVesselTypes =
        [
            VesselType.Tanker,
            VesselType.GasCarrier,
            VesselType.BulkCarrier,
            VesselType.GeneralCargoShip,
            VesselType.RefrigeratedCargoCarrier,
            VesselType.CombinationCarrier
        ];
        if (cargoMtVesselTypes.Contains(vesselType))
        {
            return "mt";
        }

        if (vesselType == VesselType.LNGCarrier)
        {
            return "m3";
        }

        List<VesselType> passengerVesselTypes =
        [
            VesselType.CruisePassengerShip,
            VesselType.RoRoPassengerShip,
            VesselType.RoRoPassengerShipHighSpeedCraftDesignedToSOLASChapterX
        ];
        if (passengerVesselTypes.Contains(vesselType))
        {
            return "person";
        }

        if (vesselType == VesselType.RoRoCargoShipVehicleCarrier)
        {
            return "m"; // TODO: 待确定单位
        }

        if (vesselType == VesselType.ContainerShip)
        {
            // Container Ship 的 TEU 被转为了体积，所以也是用 m3
            return "m3";
        }

        return "";
    }

    public static DdVector GetDdVector(VesselType vesselType, float dwt)
    {
        return vesselType switch
        {
            VesselType.BulkCarrier => new DdVector(0.86F, 0.94F, 1.06F, 1.18F),
            VesselType.GasCarrier => dwt >= 65_000F
                ? new DdVector(0.81F, 0.91F, 1.12F, 1.44F)
                : new DdVector(0.85F, 0.95F, 1.06F, 1.25F),
            VesselType.Tanker => new DdVector(0.82F, 0.93F, 1.08F, 1.28F),
            VesselType.ContainerShip => new DdVector(0.83F, 0.94F, 1.07F, 1.19F),
            VesselType.GeneralCargoShip => new DdVector(0.83F, 0.94F, 1.06F, 1.19F),
            VesselType.RefrigeratedCargoCarrier => new DdVector(0.78F, 0.91F, 1.07F, 1.20F),
            VesselType.CombinationCarrier => new DdVector(0.87F, 0.96F, 1.06F, 1.14F),
            VesselType.LNGCarrier => dwt >= 100_000F
                ? new DdVector(0.89F, 0.98F, 1.06F, 1.13F)
                : new DdVector(0.78F, 0.92F, 1.10F, 1.37F),
            VesselType.RoRoCargoShipVehicleCarrier => new DdVector(0.86F, 0.94F, 1.06F, 1.16F),
            VesselType.RoRoCargoShip => new DdVector(0.76F, 0.89F, 1.08F, 1.27F),
            VesselType.RoRoPassengerShip => new DdVector(0.76F, 0.92F, 1.14F, 1.30F),
            VesselType.RoRoPassengerShipHighSpeedCraftDesignedToSOLASChapterX => new DdVector(0.76F, 0.92F, 1.14F,
                1.30F),
            VesselType.CruisePassengerShip => new DdVector(0.87F, 0.95F, 1.06F, 1.16F),
            _ => throw new Exception($"不存在 VesselType {vesselType} 的 DdVector")
        };
    }

    public static CIIBaselineParameters GetCIIBaselineParameters(VesselType vesselType, float dwt, float gt)
    {
        return vesselType switch
        {
            VesselType.BulkCarrier => new CIIBaselineParameters(dwt >= 279000F ? 279000F : dwt, 4745.0F, 0.622F),
            VesselType.GasCarrier => new CIIBaselineParameters(dwt, dwt >= 65000F ? 14_405.0e7F : 8104.0F,
                dwt >= 65000F ? 2.071F : 0.639F),
            VesselType.Tanker => new CIIBaselineParameters(dwt, 5247.0F, 0.610F),
            VesselType.ContainerShip => new CIIBaselineParameters(dwt, 1984.0F, 0.489F),
            VesselType.GeneralCargoShip => new CIIBaselineParameters(dwt, dwt >= 20000F ? 31_948.0F : 588.0F,
                dwt >= 20000F ? 0.792F : 0.3885F),
            VesselType.RefrigeratedCargoCarrier => new CIIBaselineParameters(dwt, 4600.0F, 0.557F),
            VesselType.CombinationCarrier => new CIIBaselineParameters(dwt, 5119.0F, 0.622F),
            VesselType.LNGCarrier => new CIIBaselineParameters(dwt >= 65_000F ? dwt : 65_000F,
                dwt >= 100_000F ? 9.827F : 14_479.0e10F, dwt >= 100_000F ? 0.000F : 2.673F),
            VesselType.RoRoCargoShipVehicleCarrier => new CIIBaselineParameters(gt >= 57_700F ? 57_700F : gt,
                gt >= 30_000F ? 3627.0F : 330F, gt >= 30_000F ? 0.590F : 0.329F),
            VesselType.RoRoCargoShip => new CIIBaselineParameters(gt, 1967.0F, 0.485F),
            VesselType.RoRoPassengerShip => new CIIBaselineParameters(gt, 2023.0F, 0.460F),
            VesselType.RoRoPassengerShipHighSpeedCraftDesignedToSOLASChapterX => new CIIBaselineParameters(gt,
                4196.0F, 0.460F),
            VesselType.CruisePassengerShip => new CIIBaselineParameters(gt, 930.0F, 0.383F),
            _ => throw new Exception($"不存在 VesselType {vesselType} 的 CIIBaselineParameters")
        };
    }
}